package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {

    @Autowired
    DtsCategoryMapper categoryMapper;

    /**
     * 查看分类列表
     * 1.一级分类
     * 2.一级分类列表对应的二级列表
     * @return
     */
    @Override
    public List<CatVo> findCategoryList() {
        LambdaQueryWrapper<DtsCategory> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DtsCategory::getDeleted,false);
        wrapper1.eq(DtsCategory::getLevel,"L1");

        //获取一级分类
        List<DtsCategory> categoryList = categoryMapper.selectList(wrapper1);

        List<CatVo> catVoList1 = categoryList.stream().map(category -> {
            CatVo catVo1 = new CatVo();
            catVo1.setLabel(category.getName());
            catVo1.setValue(category.getId());

            //获取一级分类对应的二级列表
            LambdaQueryWrapper<DtsCategory> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(DtsCategory::getDeleted, false);
            wrapper2.eq(DtsCategory::getLevel, "L2");

            //获得二级列表
            List<DtsCategory> catList = categoryMapper.selectList(wrapper2);

            List<CatVo> catVoList2 = catList.stream().map(cat -> {
                CatVo catVo2 = new CatVo();
                catVo2.setLabel(cat.getName());
                catVo2.setValue(cat.getId());

                return catVo2;
            }).collect(Collectors.toList());

            //设置二级分类
            catVo1.setChildren(catVoList2);
            return catVo1;

        }).collect(Collectors.toList());

        return catVoList1;
    }

    @Override
    public DtsCategory findById(Integer categoryId) {
        DtsCategory category = categoryMapper.selectById(categoryId);
        return category;
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param id--类目id
     * @param name--类目名称
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsCategory> iPage = new Page<>(page,limit);

        QueryWrapper<DtsCategory> wrapper = new QueryWrapper<>();
        if (id != null){
            wrapper.eq("id",id);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("name",name);
        }
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        categoryMapper.selectPage(iPage,wrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return data;
    }

    /**
     * 添加
     * @param category
     */
    @Override
    public void insert(DtsCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setDeleted(false);

        categoryMapper.insert(category);

    }

    /**
     * 删除
     * @param category
     */
    @Override
    public void delete(DtsCategory category) {
        categoryMapper.deleteById(category);
    }

    /**
     * 更新
     * @param category
     */
    @Override
    public void update(DtsCategory category) {
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted,false);

        category.setUpdateTime(new Date());
        category.setAddTime(new Date());

        categoryMapper.updateById(category);
    }
}
